package com.mis.pos.repository;

import com.mis.pos.dto.AddItemDTO;
import com.mis.pos.entity.ItemEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @Description 商品DAO
 * @Author yuxiang
 * @CreatedDate 2019/10/30 17:03
 */
@Repository
public interface ItemRepository extends JpaRepository<ItemEntity,Long> , JpaSpecificationExecutor<ItemEntity> {

    /**
     * 通过id查询商品
     * @param id
     * @return
     */
    ItemEntity findItemEntityById(Long id);


    /**
     * 根据商品名称查询商品
     * 模糊查询
     * @param itemName
     * @return
     */
    @Query(value = "select * from item it where it.item_name like %?1%",nativeQuery = true)
    List<ItemEntity> findAllByItemName(String itemName);

    /**
     * 根据id删除记录
     * @param itemId
     */
    void deleteById(Long itemId);

    /**
     * 更改商品信息
     * @param id 商品id
     * @param price 商品价格
     * @return
     */
    @Query(value = "update ItemEntity it set it.price=:price where it.id=:id")
    @Transactional
    @Modifying
    ItemEntity updateEntity(@Param("id")Long id,@Param("price")Double price);

    /**
     * 分页查询商品列表
     * @param pageSize
     * @param pageNumer
     * @return
     */
    @Query(value = "select * from item it order by it.update_time desc limit ?1 offset ?2",nativeQuery = true)
    List<ItemEntity> findAllLimit(Integer pageSize,Integer pageNumer);

    @Query(value = "select count(it.id) from item it",nativeQuery = true)
    Integer countAll();

    /**
     * 修改商品下架
     * @param id
     * @return
     */
    @Query(value = "update ItemEntity it set it.isSaled=:isSaled where it.id =:id")
    @Modifying
    @Transactional
    int updateIsSaledById(@Param("id")Long id,@Param("isSaled")int isSaled);

    @Query(value = "select it.brand from ItemEntity  it group by it.brand")
    List<String> selectBrands();

}
